package be.scond.cartpool.model;

import java.util.List;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import be.scond.cartpool.exceptions.NoShoppingListFoundException;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Group {
	
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    
    @Persistent(mappedBy = "group")
    private List<User> users;
    
    @Persistent(mappedBy = "group")
    private List<ShoppingList> shoppingLists;

	@Persistent 
	private String name; 
	
	@Persistent
	private String password;
	
	@Persistent
	private boolean confirmed = false;
	

	public Group(){	
	}
	
	public ShoppingList getOpenShoppingList() throws NoShoppingListFoundException {
		try {
			ShoppingList lastAddedList = shoppingLists.get(shoppingLists.size()-1);
			if(lastAddedList.isClosed()){
				throw new NoShoppingListFoundException();				
			}
			return lastAddedList;
		} catch (NullPointerException e) {
			throw new NoShoppingListFoundException();
		} catch (IndexOutOfBoundsException e) {
			throw new NoShoppingListFoundException();
		}
	}
	
	public String toSimpleXmlString(){
		String str = "";
		str += "<group>";
		str += "<key>"+KeyFactory.keyToString(key)+"</key>";
		str += "<name>"+name+"</name>";
		str += "<password>"+password+"</password>";
		str += "<confirmed>"+confirmed+"</confirmed>";
		str += "</group>";
		return str;
	}
	
	public String toVerbalXmlString(){
		String str = "";
		str += "<group>";
		str += "<key>"+KeyFactory.keyToString(key)+"</key>";
		str += "<name>"+name+"</name>";
		str += "<password>"+password+"</password>";
		str += "<confirmed>"+confirmed+"</confirmed>";
		str += "<users>";
		for(User u : getUsers()){
			str += u.toXmlString();
		}
		str += "</users>";
		if(shoppingLists.size() > 0){
			ShoppingList lastList = shoppingLists.get(shoppingLists.size()-1);			
			str += lastList.toXmlString();
		}
		str += "</group>";
		return str;
	}
	
	
	public Key getKey() {
		return key;
	}
	public void setKey(Key key) {
		this.key = key;
	}

	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	
	public List<ShoppingList> getShoppingLists() {
		return shoppingLists;
	}
	public void setShoppingLists(List<ShoppingList> shoppingLists) {
		this.shoppingLists = shoppingLists;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isConfirmed() {
		return confirmed;
	}
	public void setConfirmed(boolean confirmed) {
		this.confirmed = confirmed;
	}
}
